package com.alibaba.yinan.graph;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @ClassName OccupiedNodeService
 * @Author mengke
 * @create 2025/8/3 16:00
 * @Description
 */
@Service
public class OccupiedNodeService {

    // 线程安全Set存储占用节点ID
    private final Set<String> occupiedNodes = ConcurrentHashMap.newKeySet();

    // 添加占用节点
    public void occupyNodes(Collection<String> nodes) {
        occupiedNodes.addAll(nodes);
    }

    // 移除占用节点
    public void releaseNodes(Collection<String> nodes) {
        occupiedNodes.removeAll(nodes);
    }

    // 获取当前占用节点快照
    public Set<String> getOccupiedNodes() {
        return Collections.unmodifiableSet(occupiedNodes);
    }

    // 清空所有占用节点（可选）
    public void clear() {
        occupiedNodes.clear();
    }
}
